package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SysRecord;
import com.ruoyi.project.system.domain.SysStock;
import com.ruoyi.project.system.domain.vo.InAndOutVo;
import com.ruoyi.project.system.mapper.SysRecordMapper;
import com.ruoyi.project.system.mapper.SysStockMapper;
import com.ruoyi.project.system.service.ISysStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 仓储货物Service业务层处理
 *
 * @author orange
 * @date 2022-04-07
 */
@Service
public class SysStockServiceImpl implements ISysStockService {
  @Autowired private SysStockMapper sysStockMapper;
  @Autowired private SysRecordMapper recordMapper;

  /**
   * 查询仓储货物
   *
   * @param stockId 仓储货物主键
   * @return 仓储货物
   */
  @Override
  public SysStock selectSysStockByStockId(Long stockId) {
    return sysStockMapper.selectSysStockByStockId(stockId);
  }

  /**
   * 查询仓储货物列表
   *
   * @param sysStock 仓储货物
   * @return 仓储货物
   */
  @Override
  public List<SysStock> selectSysStockList(SysStock sysStock) {
    return sysStockMapper.selectSysStockList(sysStock);
  }

  /**
   * 新增仓储货物
   *
   * @param sysStock 仓储货物
   * @return 结果
   */
  @Override
  public int insertSysStock(SysStock sysStock) {
    sysStock.setCreateTime(DateUtils.getNowDate());
    return sysStockMapper.insertSysStock(sysStock);
  }

  /**
   * 修改仓储货物
   *
   * @param sysStock 仓储货物
   * @return 结果
   */
  @Override
  public int updateSysStock(SysStock sysStock) {
    sysStock.setUpdateTime(DateUtils.getNowDate());
    return sysStockMapper.updateSysStock(sysStock);
  }

  /**
   * 批量删除仓储货物
   *
   * @param stockIds 需要删除的仓储货物主键
   * @return 结果
   */
  @Override
  public int deleteSysStockByStockIds(Long[] stockIds) {
    return sysStockMapper.deleteSysStockByStockIds(stockIds);
  }

  /**
   * 删除仓储货物信息
   *
   * @param stockId 仓储货物主键
   * @return 结果
   */
  @Override
  public int deleteSysStockByStockId(Long stockId) {
    return sysStockMapper.deleteSysStockByStockId(stockId);
  }

  @Override
  public int inAndOutWarehouse(InAndOutVo inAndOutVo) {

    // 查询
    SysStock sysStock = sysStockMapper.selectSysStockByStockId(inAndOutVo.getStockId());
    if (inAndOutVo.getRecordType() == 2) {
      if (sysStock.getNum() - inAndOutVo.getNum() < 0) {
        throw new ServiceException("库存不能小于0");
      }
    }

    // 修改库存
    int i1 = sysStockMapper.inAndOutWarehouse(inAndOutVo);
    // 创建新的记录
    SysRecord sysRecord = new SysRecord();

    sysRecord.setRecordType(inAndOutVo.getRecordType());
    sysRecord.setPerson(inAndOutVo.getUserId());
    sysRecord.setStockId(inAndOutVo.getStockId());
    sysRecord.setPicture(inAndOutVo.getPicture());
    sysRecord.setNum(inAndOutVo.getNum());
    sysRecord.setCreateTime(new Date());

    int i = recordMapper.insertSysRecord(sysRecord);

    return (i1 == 1 && i == 1) ? 1 : 0;
  }
}
